<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package		anime
 * @author		Tran Hoang Thien (thienhb12@gmail.com)
 * @copyright   PHP TEAM
 * @link		http://phpandmysql.net
 * @since		Version 1.0
 * @phone       0944418192
 *
 */
	class Category extends My_Controller{
		
		public function __construct(){
			parent:: __construct();
			$this->load->model('category_model');
			$this->load->library('form_validation');
			$this->load->helper('url');
			if(!$this->session->userdata("session_user") || $this->session->userdata("session_level") != 1){
				redirect(base_url()."admin/login");
			}
		}

		public function index(){
			$data['title']             = 'List category';
			$data['color']             = '#33925c';
			$data['template']          = 'category/list_category';
			$config['base_url']        = base_url('admin/category/index');
			$config['total_rows']      = $this->category_model->count_all();
			$config['per_page']        = 20;
			$config['uri_segment']     = '4';
			$config['first_link']      = '<<';
			$config['last_link']       = '>>';
			$config['first_tag_open']  = '<li class="click">';
			$config['first_tag_close'] = '</li>';
			$config['last_tag_open']   = '<li class="click">';
			$config['last_tag_close']  = '</li>';
			$config['prev_tag_open']   = '<li class="click">';
			$config['prev_tag_close']  = '</li>';
			$config['next_tag_open']   = '<li class="click">';
			$config['next_tag_close']  = '</li>';
			$config['num_tag_open']    = '<li class="click">';
			$config['num_tag_close']   = '</li>';
			$config['next_link']       = 'Next';
			$config['prev_link']       = 'Prev';
			$config['cur_tag_open']    = '<li class="active"><a>';
			$config['cur_tag_close']   = '</a></li>';
			$this->load->library("pagination");
			$this->pagination->initialize($config);
			$start                     = $this->uri->segment(4)?$this->uri->segment(4):0;
			$param                     = array();
			$param['select']           = array('idcate','namecate','nameseocate','description','status','rewrite');
			$param['limit']            = array($config['per_page'] ,$start);	
			$data['result']            = $this->category_model->get_list($param);
			$this->load->view("layout",$data);
		}


		public function list_all(){
			$data['title']             = 'List category';
			$data['color']             = '#33925c';
			$data['template']          = 'category/list_category';
			$config['base_url']        = base_url('admin/category/index');
			$config['total_rows']      = $this->category_model->count_all();
			$config['per_page']        = 20;
			$config['uri_segment']     = '4';
			$config['first_link']      = '<<';
			$config['last_link']       = '>>';
			$config['first_tag_open']  = '<li class="click">';
			$config['first_tag_close'] = '</li>';
			$config['last_tag_open']   = '<li class="click">';
			$config['last_tag_close']  = '</li>';
			$config['prev_tag_open']   = '<li class="click">';
			$config['prev_tag_close']  = '</li>';
			$config['next_tag_open']   = '<li class="click">';
			$config['next_tag_close']  = '</li>';
			$config['num_tag_open']    = '<li class="click">';
			$config['num_tag_close']   = '</li>';
			$config['next_link']       = 'Next';
			$config['prev_link']       = 'Prev';
			$config['cur_tag_open']    = '<li class="active"><a>';
			$config['cur_tag_close']   = '</a></li>';
			$this->load->library("pagination");
			$this->pagination->initialize($config);
			$start                     = $this->uri->segment(4)?$this->uri->segment(4):0;
			$param                     = array();
			$param['select']           = array('idcate','namecate','nameseocate','description','status','rewrite');
			$param['limit']            = array($config['per_page'] ,$start);	
			$data['result']            = $this->category_model->get_list($param);
			$this->load->view($data['template'],$data);
		}

		public function add(){
			$data['title']    = 'Add category';
			$data['color']    = '#33925c';
			$data['template'] = 'category/add_category';
			$check_request    = $this->input->post();
			if($check_request != NULL){
				$this->form_validation->set_rules('namecate','namecate', 'required|trim|xss_clean');
				$this->form_validation->set_rules('nameseocate','nameseocate','trim|xss_clean');
				$this->form_validation->set_rules('description', 'description','trim|xss_clean');
				if($this->form_validation->run() == FALSE){
					$data['title']    = 'Thêm mới thể loại';
					$data['color']    = '#33925c';
					$data['template'] = 'category/add_category';
					$this->load->view('layout',$data);
				}else{
					$namecate    = fillter($this->input->post('namecate'));
					$nameseocate = fillter($this->input->post('nameseocate'));
					$description = fillter($this->input->post('description'));
					$rewrite     = str_replace(' ', '-',strtolower(str_replace('%','n',replace($namecate))));
					if($this->input->post('status') != NULL){
						$status  = $this->input->post('status');
					}else{
						$status = 2;
					}
					
					$db = array(
						'namecate'    => $namecate,
						'nameseocate' => $nameseocate,
						'description' => $description,
						'rewrite'     => $rewrite,
						'status'      => $status,
					);

					$this->category_model->save($id = false,$db);
					redirect(base_url("admin/category"));
				}
			}else{
				$this->load->view('layout',$data);
			}
		}

		public function edit(){
			$data['title']    = "Edit category";
			$data['color']    = "#33925c";
			$data['template'] = 'category/edit_category';
			$var              = array();
			$id               = $this->uri->segment(4);
			if(isset($id)){
				if($this->input->post() != NULL){
					$id = $this->uri->segment(4);
					$this->form_validation->set_rules('namecate','namecate', 'required|trim|xss_clean');
					$this->form_validation->set_rules('nameseocate','nameseocate', 'trim|xss_clean');
					$this->form_validation->set_rules('description', 'description','trim|xss_clean');
					if($this->form_validation->run() == FALSE){
						$data['title']    = 'Sửa thông tin';
						$data['color']    = '#33925c';
						$data['template'] = 'category/edit_category';
						$param            = array('namecate','nameseocate','description','status','idcate');
						$data['result']   = $this->category_model->get_info($id,$param);
						$this->load->view('layout',$data);
					}else{
						$namecate    = fillter($this->input->post('namecate'));
						$nameseocate = fillter($this->input->post('nameseocate'));
						$description = fillter($this->input->post('description'));
						$rewrite     = str_replace(' ', '-',strtolower(str_replace('%','n',replace($namecate))));
						if($this->input->post('status') != NULL){
							$status  = $this->input->post('status');
						}else{
							$status = 2;
						}
						
						$db = array(
							'namecate'    => $namecate,
							'nameseocate' => $nameseocate,
							'description' => $description,
							'rewrite'     => $rewrite,
							'status'      => $status,
						);

						$this->category_model->save($id,$db);
						redirect(base_url("admin/category"));
					}
				}else{
					if($id != NULL){
						$param          = array('namecate','nameseocate','description','status','idcate');
						$data['result'] = $this->category_model->get_info($id,$param);
						$this->load->view('layout',$data);
					}
				}
			}else{
				redirect(base_url("admin/category"));
			}
		}

		public function delete(){
			$id = $this->uri->segment(4);
			if($id != NULL){	
				$this->category_model->delete($id);
				redirect(base_url("admin/category"));
			}
		}

		public function change_status(){
			if($this->input->post('action') != NULL){
				$data = array();
				$id   = (int)$this->input->post('id');
				if($this->input->post('status') == 'publish'){
					$data['status'] = 1;
					$this->category_model->save($id,$data);
				}elseif($this->input->post('status') == 'unpublish'){
					$data['status'] = 2;
					$this->category_model->save($id,$data);
				}
			}
		}

		public function bulk_action(){
			$var      = array();
			$request  = $this->input->post();
			if(!empty($request)){
				$type = $this->uri->segment(4);
				$uid  = $this->input->post('id');
				if($type == 'delete'){
					if(!empty($uid)){
						foreach($uid as $value){
							$param = array('idcate' => $value);
							$this->category_model->delete_rule($param);
						}
					}
					$var['action'] = 'delete';
				}elseif($type == 'publish'){
					if(!empty($uid)){
						foreach($uid as $value){
							$where = array('idcate'=> $value);
							$data  = array('status' => 1) ;
							$this->category_model->update_rule($where,$data);
						}
					}
					$var['action'] = 'publish';
				}elseif($type == 'unpublish'){
					if(!empty($uid)){
						foreach($uid as $value){
							$where = array('idcate'=> $value);
							$data  = array('status' => 2) ;
							$this->category_model->update_rule($where,$data);
						}
					}
					$var['action'] = 'unpublish';
				}
				$var['error'] = 0;
			}else{
				$var['error'] = 1;
			}
			$var['url'] = base_url('admin/category/list_all');
			exit(json_encode($var));
		}
	}